package com.google.project.client.HumanResources;

import java.util.Date;
import java.util.List;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.project.client.Service.ProjectStaffService;
import com.google.project.client.Service.ProjectStaffServiceAsync;
import com.google.project.shared.CommonFuntion;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.ProjectStaffDTO;
import com.google.gwt.user.client.ui.Label;

public class HMProjectStaffList extends Composite {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField(provided = true) CellTable<ProjectStaffDTO> ctbProjectStaffList = new CellTable<ProjectStaffDTO>();
	@UiField SimplePager pager;
	@UiField Button btnBack;
	@UiField Button btnTaskReq;
	@UiField Label lblProjectName;
	
	private final ProjectStaffServiceAsync projectStaffService = GWT.create(ProjectStaffService.class);
	interface Binder extends UiBinder<Widget, HMProjectStaffList> {
	}

	public HMProjectStaffList() {

	}

	private String projectId;
	private ProjectDTO projectDTO=new ProjectDTO();
	public HMProjectStaffList(ProjectDTO projectDTO) {
		this.projectDTO=projectDTO;
		this.projectId = projectDTO.getProjectID();
		initWidget(binder.createAndBindUi(this));
		lblProjectName.setText("Dự án: "+projectDTO.getProjectName());
		bindData(projectId);
	}

	public void bindData(String projectId) {
		projectStaffService.getProjectStaffList(projectId, new AsyncCallback<List<ProjectStaffDTO>>() {

					@Override
					public void onSuccess(List<ProjectStaffDTO> result) {
						if(result.size() == 0) {
							Window.alert("Không có dữ liệu!");
						}
						loadData(result);
						initTable();
					}

					@Override
					public void onFailure(Throwable caught) {
					}
				});

	}
	
	protected void loadData(List<ProjectStaffDTO> projectStaffDTO) {
		
		ListDataProvider<ProjectStaffDTO> dataProvider = new ListDataProvider<ProjectStaffDTO>();

		// Add the data to the data provider, which automatically pushes it to
		// the widget
		List<ProjectStaffDTO> list = dataProvider.getList();
		for(int i = 0; i < projectStaffDTO.size(); i++) {
			projectStaffDTO.get(i).setIndex(i + 1);
			list.add(projectStaffDTO.get(i));
		}
		pager.setDisplay(ctbProjectStaffList);
		// Connect the table to the data provider.
		dataProvider.addDataDisplay(ctbProjectStaffList);
	}
	
	public void initTable() {
		//STT
		TextColumn<ProjectStaffDTO> indexColumn = new TextColumn<ProjectStaffDTO>() {
			@Override
			public String getValue(ProjectStaffDTO object) {
				return object.getIndex()+"";
			}
		};
		//Nhan vien Id
		TextColumn<ProjectStaffDTO> userCodeColumn = new TextColumn<ProjectStaffDTO>() {
			@Override
			public String getValue(ProjectStaffDTO userId) {
				return userId.getUserCode();
			}
		};

		//Ten nhan vien
		TextColumn<ProjectStaffDTO> userNameColumn = new TextColumn<ProjectStaffDTO>() {
			@Override
			public String getValue(ProjectStaffDTO user) {
				return user.getUserName();
			}
		};

		//Position
		TextColumn<ProjectStaffDTO> positionColumn = new TextColumn<ProjectStaffDTO>() {
			@Override
			public String getValue(ProjectStaffDTO staff) {
				int position = staff.getPosition();
				String result = "";
				if(position == 1) {
					result += "Giám đốc";
				} else if(position == 2) {
					result += "Quản lý";
				} else if(position == 3) {
					result += "Chỉ huy công trường";
				} else if(position == 4) {
					result += "Quản lý nhân sự";
				} else if(position == 5) {
					result += "Nhân viên";
				} else if(position == 6) {
					result += "Admin";
				}
				return result;
			}
		};
		//Start date
		TextColumn<ProjectStaffDTO> startDateColumn = new TextColumn<ProjectStaffDTO>() {
			@Override
			public String getValue(ProjectStaffDTO staff) {
				// Cho nay can phai format lai date
				return CommonFuntion.convertToDate(new Date(staff.getStartDate()));
			}
		};
		//Finish date
		TextColumn<ProjectStaffDTO> finishDateColumn = new TextColumn<ProjectStaffDTO>() {
			@Override
			public String getValue(ProjectStaffDTO staff) {
				// Cho nay can phai format lai date
				return CommonFuntion.convertToDate(new Date(staff.getFinishtDate()));
			}
		};
		// Tinh trang
		TextColumn<ProjectStaffDTO> statusColumn = new TextColumn<ProjectStaffDTO>() {
			@Override
			public String getValue(ProjectStaffDTO user) {
				int status = user.getStatus();
				String _status = "";
				if (status == 0) {
					_status += "Chưa tham gia";
				} else if (status == 1) {
					_status += "Đang tham gia";
				} else if (status == 2) {
					_status += "Đã hoàn thành";
				}
				return _status;
			}
		};

		ctbProjectStaffList.setWidth("100%", true);
		ctbProjectStaffList.setPageSize(5);
		ctbProjectStaffList.setPageStart(0);
		ctbProjectStaffList.setStyleName("ttable");
		
		ctbProjectStaffList.addColumn(indexColumn, "STT");
		ctbProjectStaffList.setColumnWidth(indexColumn, 40.0, Unit.PX);
		
		ctbProjectStaffList.addColumn(userCodeColumn, "Mã nhân viên");
		ctbProjectStaffList.setColumnWidth(userCodeColumn, 20.0, Unit.PCT);
		ctbProjectStaffList.getColumn(ctbProjectStaffList.getColumnIndex(userCodeColumn)).setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
		
		ctbProjectStaffList.addColumn(userNameColumn, "Tên nhân viên");
		ctbProjectStaffList.setColumnWidth(userNameColumn, 35.0, Unit.PCT);
		ctbProjectStaffList.getColumn(ctbProjectStaffList.getColumnIndex(userNameColumn)).setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
		
		ctbProjectStaffList.addColumn(positionColumn, "Vị trí");
		ctbProjectStaffList.setColumnWidth(positionColumn, 15.0, Unit.PCT);
		ctbProjectStaffList.getColumn(ctbProjectStaffList.getColumnIndex(positionColumn)).setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
		
		ctbProjectStaffList.addColumn(startDateColumn, "Ngày bắt đầu");
		ctbProjectStaffList.setColumnWidth(startDateColumn, 17.0, Unit.PCT);
		ctbProjectStaffList.getColumn(ctbProjectStaffList.getColumnIndex(startDateColumn)).setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
		
		ctbProjectStaffList.addColumn(finishDateColumn, "Ngày kết thúc");
		ctbProjectStaffList.setColumnWidth(finishDateColumn, 17.0, Unit.PCT);
		ctbProjectStaffList.getColumn(ctbProjectStaffList.getColumnIndex(finishDateColumn)).setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
		 
		ctbProjectStaffList.addColumn(statusColumn, "Trạng thái");
		ctbProjectStaffList.setColumnWidth(statusColumn, 17.0, Unit.PCT);
		ctbProjectStaffList.getColumn(ctbProjectStaffList.getColumnIndex(statusColumn)).setHorizontalAlignment(HasAlignment.ALIGN_LEFT);
	}
	
	@UiHandler("btnBack")
	void onBtnBackClick(ClickEvent event) {
		HMEntrypoint.get().HMProjectDetail(projectId);
	}
	@UiHandler("btnTaskReq")
	void onBtnTaskReqClick(ClickEvent event) {
		HMEntrypoint.get().HMHumanReqDetail(projectDTO);
	}
}
